package com.edfu.core.commons.service.ws;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;

import com.edfu.core.commons.beans.EduObjectType;
import com.edfu.core.commons.beans.Rating;
import com.edfu.core.commons.service.api.IRatingService;

@Path("/")
@Produces( { MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_XML)
@WebService(targetNamespace = "http://com.edfu.core.commons.service.ws.RatingService")
public class RatingServiceDelegate {
	
	@Autowired
	private IRatingService ratingService;
	
	@POST
	@Path("/ratings")
	@WebMethod(operationName = "addRating")
	public Response addRating(Rating rating) {
		ratingService.addRating(rating); 
		return Response.ok().build();
	}
	
	@GET
	@Path("/ratings/mean/objectId/{objectId}/objectType/{objectType}")
	@WebMethod(operationName = "getMeanRating")
	public double getMeanRating(@PathParam(value = "objectId") long objectId, @PathParam(value = "objectType") String objectType) {
		return ratingService.getMeanRating(objectId, EduObjectType.valueOf(objectType));
	}
	
	@GET
	@Path("/ratings/count/objectId/{objectId}/objectType/{objectType}")
	@WebMethod(operationName = "getRatingCount")
	public long getRatingCount(@PathParam(value = "objectId") long objectId, @PathParam(value = "objectType") String objectType) {
		return ratingService.getRatingCount(objectId, EduObjectType.valueOf(objectType));
	}
	
	@GET
	@Path("/ratings/objectId/{objectId}/objectType/{objectType}")
	@WebMethod(operationName = "getRatings")
	public Response getRatings(@PathParam(value = "objectId") long objectId, @PathParam(value = "objectType") String objectType) {
		List<Rating> ratings = ratingService.getRatings(objectId, EduObjectType.valueOf(objectType));
		return Response.ok(ratings).build();
	}
	
	@GET
	@Path("/ratings/user/{userId}/objectId/{objectId}/objectType/{objectType}")
	@WebMethod(operationName = "getUserRating")
	public Response getUserRating(@PathParam(value = "userId") String userId, @PathParam(value = "objectId") long objectId, @PathParam(value = "objectType") String objectType) {
		Rating rating = ratingService.getUserRating(userId, objectId, EduObjectType.valueOf(objectType));
		return Response.ok(rating).build();
	}

}
